<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="120px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" placeholder="=排序号"></el-input-number>
			</el-form-item>
			<el-form-item label="数据源名称" prop="dbSourceName">
				<el-input v-model="form.dbSourceName" placeholder="数据源名称" maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="数据源类型" prop="code">
				<el-select v-model="form.dbSourceType">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="数据源链接" prop="dbLink">
				<el-input v-model="form.dbLink" placeholder="数据源链接" maxlength="180"></el-input>
			</el-form-item>
			<el-form-item label="数据库用户名" prop="dbUserName">
				<el-input v-model="form.dbUserName" placeholder="数据库用户名" maxlength="100"></el-input>
			</el-form-item>
			<el-form-item label="数据库密码" prop="dbPassWord">
				<el-input v-model="form.dbPassWord" placeholder="数据库密码" maxlength="100"></el-input>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.remark" maxlength="380" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
			<el-button @click="visible=false">取消</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑'
			},
			options: [
				{value: "sys", label: '当前系统'},
				{value: "mysql", label: 'MYSQL'},
				{value: "oracle", label: 'ORACLE'},
				{value: "mssql", label: 'MSSQL'},
				{value: "kb", label: '人大金仓V8'},
				{value: "dmdbms", label: '国产达梦'},
				{value: "oscar", label: '国产神通'}
			],
			form: {
				dbSourceId: "",
				sortNo: 0,
				dbSourceName: '',
				dbSourceType: '',
				dbLink: '',
				dbUserName: '',
				dbPassWord: '',
				remark: ''
			},
			visible: false,
			isSaveing: false,
			rules: {
				dbSourceName: [
					{required: true, message: '数据源名称不能为空', trigger: 'blur'}
				],
				dbLink: [
					{required: true, message: '数据源链接不能为空', trigger: 'blur'}
				],
				dbUserName: [
					{required: true, message: '数据库用户名不能为空', trigger: 'blur'}
				],
				dbPassWord: [
					{required: true, message: '数据库密码不能为空', trigger: 'blur'}
				]
			},
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.bigdata.bigDataSource.insertBigDataSource.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.bigdata.bigDataSource.updateBigDataSource.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.dbSourceId = data.dbSourceId;
			this.form.sortNo = data.sortNo;
			this.form.dbSourceName = data.dbSourceName;
			this.form.dbSourceType = data.dbSourceType;
			this.form.dbLink = data.dbLink;
			this.form.dbUserName = data.dbUserName;
			this.form.dbPassWord = data.dbPassWord;
			this.form.remark = data.remark
		}
	}
}
</script>

<style>
</style>
